Skip to content

inspector: load network tracking modules eagerly#64379

Open
harjothkhara wants to merge 1 commit into
nodejs:mainfrom
harjothkhara:fix/inspector-network-tracking-eager-load
Open

inspector: load network tracking modules eagerly#64379
harjothkhara wants to merge 1 commit into
nodejs:mainfrom
harjothkhara:fix/inspector-network-tracking-eager-load

Conversation

@harjothkhara

Copy link
Copy Markdown

Summary

--experimental-network-inspection can abort the whole Node.js process when a debugger attaches. Reported in #64308.

Network.enable is delivered to the main thread via a V8 interrupt that can run in the middle of arbitrary JavaScript execution β€” including while another module is still being loaded by require(). The network-tracking enable()/disable() helpers lazily require()d their modules (network_http, network_http2, network_undici), which transitively pull in inspector β†’ worker_threads β†’ internal/worker/io β†’ stream. When the interrupt lands mid-require(), require() returns a half-initialized module and throws β€” for example Class extends value undefined from internal/worker/io, or require(...).enable is not a function. Agent::ToggleNetworkTracking (src/inspector_agent.cc) treats any exception thrown while toggling network tracking as unrecoverable and calls UNREACHABLE(), aborting the process.

Fix

Load the three network-tracking modules eagerly at module scope, so enable()/disable() never call require() from an interrupt. The requires now run during setupNetworkInspection() at bootstrap, before the inspector can dispatch Network.enable. This only affects the opt-in --experimental-network-inspection flag, where an inspector is already active.

Verification

Minimal repro from the issue:

node --inspect-wait --experimental-network-inspection -e "require('net')"

then attach a debugger and send Network.enable.

Driving the real inspector path locally (attach over WebSocket, Runtime.runIfWaitingForDebugger, then Network.enable with jitter to spray the interrupt across the require window):

  • unpatched main build: aborts (Cannot toggle network tracking β†’ UNREACHABLE), reproduced at iteration 1
  • patched build: 0 crashes over 100 iterations

Adds test/parallel/test-inspector-network-tracking-eager-load.js, which asserts the network-tracking modules are loaded at setup and that enable()/disable() do not lazily load any module.

Note

A deeper hardening β€” not calling UNREACHABLE() in ToggleNetworkTracking when the toggle callback throws β€” would defend against this class of failure at the C++ layer as well. I kept this PR to the minimal JS change that fixes the reported crash, but I'm happy to add that as a follow-up if maintainers prefer.

Fixes: #64308

`Network.enable` is dispatched to the main thread through a V8 interrupt
that can run in the middle of arbitrary JavaScript execution, including
while another module is still being loaded by require(). The network
tracking enable()/disable() helpers lazily require()d their modules,
which pull in `inspector`, `worker_threads` and `stream`. When the
interrupt landed mid-require(), require() returned a half-initialized
module and threw (for example "Class extends value undefined" from
internal/worker/io, or "require(...).enable is not a function"). The
inspector agent treats any exception thrown while toggling network
tracking as unrecoverable and aborts the whole process.

Load the three network tracking modules eagerly at setup time so that
enable()/disable() never call require() from an interrupt. They now run
during setupNetworkInspection() at bootstrap, before the inspector can
dispatch Network.enable.

Fixes: nodejs#64308
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/inspector

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unreachable code reached, Cannot toggle network tracking

2 participants